<?php

declare(strict_types=1);

namespace app\xapply\controller;

use app\xapply\middleware\Auth;
use think\facade\Db;
use app\xapply\Xapply;

class Bind extends Xapply
{

    protected $middleware = [Auth::class];

    /**
     * 绑定推送Token
     * url: http://域名/xapply/bind
     * doc: http://lutui.net/doc/app/bind.html
     */
    public function index()
    {
        $user = Db::name('Admin')->find(XAPPLY_UID);
        $data = [
            'last_login_time' => time(),
            'last_login_ip' => request()->ip(),
            'login_num' => $user['login_num'] + 1,
        ];
        Db::name('Admin')->where(['id' => $user['id']])->update($data);
        $token = make_token().'-'.get_codeno('xapply-').'-'.set_salt();
        Auth::update([
            'user_id' => $user['id'],
            'token' => $token,
            'push' => $this->data['pushToken'],
            'lang' => $this->data['lang'],
            'device' => $this->data['device'],
        ]);
        return json(['error' => '', 'token' => $token]);
    }
}
